package com.jiang.myfm.fragment;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jiang.myfm.R;
import com.jiang.myfm.adapters.BillboardInfoAdapter;
import com.jiang.myfm.entities.billboard.DataEntity;
import com.jiang.myfm.utils.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BillboardInfoMenuFragment extends Fragment implements Callback<DataEntity>,View.OnClickListener,ViewPager.OnPageChangeListener{

    private List<String> titleList,keyList;
    private DataEntity datas;
    private ViewPager viewPager;
    private TabLayout tab;
    private BillboardInfoAdapter adapter;
    private Button more_bte;
    private MoreWindow window;
    private int rbIndex;
    private String contentType;


    public static BillboardInfoMenuFragment newInstance(String contentType,String key) {
        Bundle args = new Bundle();
        args.putString("contentType",contentType);
        args.putString("key",key);
        BillboardInfoMenuFragment fragment = new BillboardInfoMenuFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleList = new ArrayList<>();
        keyList = new ArrayList<>();
        titleList.add("总榜");

        Bundle bundle = getArguments();
        contentType = bundle.getString("contentType");
        String key = bundle.getString("key");
        keyList.add(key);

        HttpService httpService = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        httpService.getData(contentType,"android",key,1,20,2).enqueue(this);

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_billboard_info_menu, container, false);
        viewPager = ((ViewPager) view.findViewById(R.id.billboardinfomenu_vp));
        viewPager.addOnPageChangeListener(this);
        tab = ((TabLayout) view.findViewById(R.id.billboard_tab));
        more_bte = ((Button) view.findViewById(R.id.more_btn));
        more_bte.setOnClickListener(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onResponse(Call<DataEntity> call, Response<DataEntity> response) {
        datas = response.body();
        for (int i = 0; i < datas.getCategories().size(); i++) {
            titleList.add(datas.getCategories().get(i).getName());
            keyList.add(datas.getCategories().get(i).getKey());
        }
        adapter = new BillboardInfoAdapter(getFragmentManager(),titleList,contentType,keyList);
        viewPager.setAdapter(adapter);
        tab.setupWithViewPager(viewPager);
    }

    @Override
    public void onFailure(Call<DataEntity> call, Throwable t) {

    }

    @Override
    public void onClick(View view) {
        window = new MoreWindow(getContext(),rbIndex);
        ColorDrawable color = new ColorDrawable(Color.TRANSPARENT);
        window.setBackgroundDrawable(color);
        window.showAsDropDown(view,-100,-100);
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
                params.alpha = 1f;
                getActivity().getWindow().setAttributes(params);
            }
        });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        rbIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class MoreWindow extends PopupWindow implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{

        private Context context;
        private WindowManager.LayoutParams params;
        private RadioGroup moreGroup;
        private int index;


        public MoreWindow(Context context,int index) {
            super(context);
            this.context = context;
            this.index = index;
            init();
        }

        private void init() {
            View view = LayoutInflater.from(context).inflate(R.layout.more_window,null);
            setContentView(view);

            view.findViewById(R.id.close_btn).setOnClickListener(this);
            moreGroup = ((RadioGroup) view.findViewById(R.id.more_group));
            RadioButton radioButton = (RadioButton) moreGroup.getChildAt(index);
            radioButton.setChecked(true);
            radioButton.setClickable(true);
            moreGroup.setOnCheckedChangeListener(this);
            setWidth(WindowManager.LayoutParams.MATCH_PARENT);
            setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

            params = getActivity().getWindow().getAttributes();
            params.alpha = 0.7f;
            getActivity().getWindow().setAttributes(params);
            setOutsideTouchable(true);

        }

        @Override
        public void onClick(View view) {
            dismiss();
        }

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {
                case R.id.all:
                    dismiss();
                    viewPager.setCurrentItem(0);
                break;
                case R.id.youshengshu:
                    dismiss();
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.music:
                    dismiss();
                    viewPager.setCurrentItem(2);
                    break;
                case R.id.yule:
                    dismiss();
                    viewPager.setCurrentItem(3);
                    break;
                case R.id.xiangsheng:
                    dismiss();
                    viewPager.setCurrentItem(4);
                    break;
                case R.id.child:
                    dismiss();
                    viewPager.setCurrentItem(5);
                    break;
                case R.id.zixun:
                    dismiss();
                    viewPager.setCurrentItem(6);
                    break;
                case R.id.qinggan:
                    dismiss();
                    viewPager.setCurrentItem(7);
                    break;
                case R.id.history:
                    dismiss();
                    viewPager.setCurrentItem(8);
                    break;
                case R.id.waiyu:
                    dismiss();
                    viewPager.setCurrentItem(9);
                    break;
                case R.id.educate:
                    dismiss();
                    viewPager.setCurrentItem(10);
                    break;
                case R.id.baijia:
                    dismiss();
                    viewPager.setCurrentItem(11);
                    break;
                case R.id.radio:
                    dismiss();
                    viewPager.setCurrentItem(12);
                    break;case R.id.opera:
                    dismiss();
                    viewPager.setCurrentItem(13);
                    break;
                case R.id.bussiness:
                    dismiss();
                    viewPager.setCurrentItem(14);
                    break;
                case R.id.IT:
                    dismiss();
                    viewPager.setCurrentItem(15);
                    break;
                case R.id.health:
                    dismiss();
                    viewPager.setCurrentItem(16);
                    break;
                case R.id.school:
                    dismiss();
                    viewPager.setCurrentItem(17);
                    break;
                case R.id.tour:
                    dismiss();
                    viewPager.setCurrentItem(18);
                    break;
                case R.id.car:
                    dismiss();
                    viewPager.setCurrentItem(19);
                    break;
                case R.id.games:
                    dismiss();
                    viewPager.setCurrentItem(20);
                    break;
                case R.id.video:
                    dismiss();
                    viewPager.setCurrentItem(21);
                    break;
                case R.id.fashion:
                    dismiss();
                    viewPager.setCurrentItem(22);
                    break;

            }
        }
    }
}
